﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Oplaco.Core.BusinessLogicLayer;
using System.Data;
using Oplaco.Core.Configuration;

namespace Oplaco.Web.DTO
{
    /// <summary>
    /// Custom collection object
    /// </summary>
    public class CachedAdvertismentCollection : ICollection<CachedAdvertisment>
    {
        
        private List<CachedAdvertisment> innerCollection;
        private bool isRO = false;
               

        /// <summary>
        /// The amount of items in the collection
        /// </summary>
        public int Count
        {
            get
            {
                if (innerCollection == null) return -1;
                return innerCollection.Count;             
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public bool IsReadOnly
        {
            get { return isRO; }
        }

        #region custom contructors

        // Adds an index to the collection.
        public CachedAdvertisment this[int index]
        {
            get { return (CachedAdvertisment)innerCollection[index]; }
            set { innerCollection[index] = value; }
        }

        public CachedAdvertismentCollection()
        {
            innerCollection = new List<CachedAdvertisment>();
        }     

        
        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        public void Add(CachedAdvertisment item)
        {
            if (!Contains(item)) {
                innerCollection.Add(item);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void Clear()
        {
            innerCollection.Clear();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Contains(CachedAdvertisment item)
        {
            CachedAdvertisment query = (from i in innerCollection where i.Equals(item) select i).SingleOrDefault<CachedAdvertisment>();
            return query == null ? true : false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="array"></param>
        /// <param name="arrayIndex"></param>
        public void CopyTo(CachedAdvertisment[] array, int arrayIndex)
        {
            innerCollection = array.Select(item => (CachedAdvertisment)item).ToList<CachedAdvertisment>();
        }
              
        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Remove(CachedAdvertisment item)
        {
            CachedAdvertisment query = (from i in innerCollection where i.Equals(item) select i).SingleOrDefault<CachedAdvertisment>();
            if (query == null) return false;
            else innerCollection.RemoveAt(item.Rank);

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IEnumerator<CachedAdvertisment> GetEnumerator()
        {
            return new CachedAdvertismentEnumerator(this);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return new CachedAdvertismentEnumerator(this);
        }


       

        /// <summary>
        /// Defines the enumerator for the Boxes collection.
        // (Some prefer this class outside in the collection class.)
        /// </summary>
        public class CachedAdvertismentEnumerator : IEnumerator<CachedAdvertisment>
        {
            private CachedAdvertismentCollection collection;
            private int curIndex;
            private CachedAdvertisment curAd;

            /// <summary>
            /// custom constructor
            /// </summary>
            /// <param name="coll"></param>
            public CachedAdvertismentEnumerator(CachedAdvertismentCollection coll) {
                collection = coll;
                curIndex = -1;
                curAd = default(CachedAdvertisment);
            }

            /// <summary>
            /// 
            /// </summary>
            public CachedAdvertisment Current
            {
                get { return curAd; }
            }

            /// <summary>
            /// 
            /// </summary>
            public void Dispose()
            {
                
            }

            /// <summary>
            /// 
            /// </summary>
            object System.Collections.IEnumerator.Current
            {
                get { return curAd; }
            }
                        
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public bool MoveNext()
            {
                
                if (++curIndex >= collection.Count)
                {
                    return false;
                }
                else {
                    curAd = collection[curIndex]; //(from item in collection where item.Rank == curIndex select item).SingleOrDefault<CachedAdvertisment>();
                }
                return true;
            }

            /// <summary>
            /// 
            /// </summary>
            public void Reset()
            {
                curIndex = -1;
            }
        }

        
    }
}
